package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjb.vr.common.dto.SysUpgradeDevLog;
import com.bjb.vr.common.service.ResDeviceService;
import com.bjb.vr.common.service.SysUpgradeDevLogService;
import com.bjb.vr.common.utils.RedisUtil;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.redis.constants.RedisConstants;
import com.bjb.vr.remote.ws.websocket.entity.Action;
import com.bjb.vr.remote.ws.websocket.entity.Command;
import com.bjb.vr.remote.ws.websocket.entity.SingleModeParameter;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @version 1.0.0
 * @description:重新升级失败的设备
 * @author: caihaibo
 * @createDate: 2022/7/29
 */
@Slf4j
@Component
public class VRApkReUpdateMessageHandler implements MessageHandler {

    @Autowired
    ResDeviceService resDeviceService;

    @Autowired
    SysUpgradeDevLogService sysUpgradeDevLogService;

    @Autowired
    RedisUtil redisUtil;

    final String  LOCK_KEY = "res_dev_sys_re_update_id";

    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {
        String dbLockKey = RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY + LOCK_KEY+ rabbitWebSocketMsg.getGroupCode();

        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);

        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_VR_APK_RE_UPDATE)){
            log.info("头盔大厅重新升级消息");
            JSONObject _jsonObject = JSONObject.parseObject(message.getCommand().getParameter());
            long sysUpdateId = _jsonObject.getLong("sysUpdateId");
            JSONArray jsonArray = _jsonObject.getJSONArray("devUdids");

            if(jsonArray!=null  && jsonArray.size()>0){
                String value = String.valueOf(System.currentTimeMillis());
                if (redisUtil.lock(dbLockKey, value, RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY_EXPIRE_TIME)) {
                    for(int i=0 ; i<jsonArray.size() ; i++) {
                        String _udid = (String) jsonArray.get(i);
                        resDeviceService.updateResDeviceSysUpdateId(rabbitWebSocketMsg.getGroupCode(), sysUpdateId, rabbitWebSocketMsg.getTenantId(), _udid);
                        sysUpgradeDevLogService.reUpdSysUpGradeDevLog(rabbitWebSocketMsg.getGroupCode(), rabbitWebSocketMsg.getTenantId(), sysUpdateId,_udid);
                    }
                    redisUtil.unlock(dbLockKey, value);
                }

                //发送重启命令
                Action action = new Action();
                action.setAction(MessageActionConstants.MESSAGE_ACTION_RESTART_VR);
                String udid = "";
                Command command = new Command<>();
                for(int i=0 ; i<jsonArray.size() ; i++){
                    command = new Command<>();
                    udid = (String) jsonArray.get(i);
                    command.setUdid(udid);
                    action.setCommand(command);
                    rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
                    message.getCommand().setUdid(udid);
                    WebSocketMsgHandler.sendSingleClient(rabbitWebSocketMsg,message);
                }
                //设置为空消息，防止其他handler处理
                action.setAction(MessageActionConstants.MESSAGE_ACTION_EMPTY);
                rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
            }

        }
    }
}
